﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using HappyFrogXna;
using HappyFrogXna.ScreenSystem;

namespace HappyFrog
{
    public partial class GamePage : PhoneApplicationPage
    {
        private ContentManager _contentManager;
        private GameTimer _timer;
        private SpriteBatch _spriteBatch;

        #region Xna
        private GameMainScreen _gamePlayScreen;
        private InputHelper _input;
        #endregion

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            _contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            _timer = new GameTimer();
            _timer.UpdateInterval = TimeSpan.FromTicks(333333);
            _timer.Update += OnUpdate;
            _timer.Draw += OnDraw;

            #region Xna
            ConvertUnits.SetDisplayUnitToSimUnitRatio(16f);
            _contentManager.RootDirectory = "Content";

            _input = new InputHelper();
            #endregion
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            #region Xna
            _gamePlayScreen = new GameMainScreen();
            GolbalXnaGadgetProvider.Instance.TopScreen = _gamePlayScreen;
            GolbalXnaGadgetProvider.Instance.Content = _contentManager;
            GolbalXnaGadgetProvider.Instance.GraphicsDevice = SharedGraphicsDeviceManager.Current.GraphicsDevice;
            GolbalXnaGadgetProvider.Instance.SpriteBatch = _spriteBatch;
            _gamePlayScreen.Exit = NavigationService.GoBack;
            _gamePlayScreen.RemoveScreen = (GameScreen screen) => { };
            _gamePlayScreen.ResetElapsedTime = () => { };

            _gamePlayScreen.LoadContent();
            _input.LoadContent();
            #endregion

            // Start the timer
            _timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            _timer.Stop();

            #region Xna
            _gamePlayScreen.UnloadContent();
            #endregion

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            #region Xna
            GameTime gameTime = new GameTime(e.TotalTime, e.ElapsedTime);

            // Read the keyboard and gamepad.
            _input.Update(gameTime);

            // Update the screen.
            _gamePlayScreen.Update(gameTime, false, false);

            _input.ShowCursor = _gamePlayScreen.HasCursor;
            _input.EnableVirtualStick = _gamePlayScreen.HasVirtualStick;
            _gamePlayScreen.HandleInput(_input, gameTime);
            #endregion

            FrameworkDispatcher.Update();
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);

            #region Xna
            GameTime gameTime = new GameTime(e.TotalTime, e.ElapsedTime);
            _gamePlayScreen.Draw(gameTime);
            _input.Draw();
            #endregion
        }
    }
}
